package com.vip.web.controller;


import com.vip.dao.action.domain.SysOperLog;
import com.vip.dao.admin.domain.SysPermission;
import com.vip.dao.admin.domain.SysRole;
import com.vip.dao.admin.domain.User;
import com.vip.service.admin.SysPermissionService;
import com.vip.service.admin.SysRoleService;
import com.vip.service.admin.UserService;
import com.vip.service.kafka.KafkaProducer;
import com.vip.web.dto.QueryInfo;
import com.vip.web.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Api(value = "用户,角色,权限")
@RestController
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private KafkaProducer kafkaProducer;

    @ResponseBody
    @RequestMapping(value = "/admin/test",method = RequestMethod.GET)
    public String test(){
        return "hello";
    }


    /**
     *
     * pageNum  第几页
     * pageSize  每页的数量
     * @return
     */
    @ApiOperation(value = "查找所有用户")
    @ResponseBody
    @RequestMapping(value = "/admin/findAllUser/{num}/{size}",method = RequestMethod.GET)
    public Result findAllUser(@PathVariable("num") int num, @PathVariable("size") int size){
        QueryInfo queryInfo = new QueryInfo(num,size);
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        Page<User> page = userService.findAlluser(pageNum,pageSize);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","获取用户列表成功");
        map.put("status",200);
        return Result.success(page,map);
    }



    @ApiOperation(value = "根据id查找用户，redis做缓存")
    @ResponseBody
    @RequestMapping(value = "/admin/findAllUser/{id}",method = RequestMethod.GET)
    public Result findUserById(@PathVariable("id") Integer id){
        Long userId = id.longValue();
        User user = userService.findUserById(userId);
        if( null != user){
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","查找用户成功");
            map.put("status",200);
            return Result.success(user,map);
        }else{
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","查找用户为空");
            map.put("status",250);
            return Result.success(null,map);
        }
    }

    @ApiOperation(value = "根据id查找用户2,未做缓存")
    @ResponseBody
    @RequestMapping(value = "/admin/findAllUser2/{id}",method = RequestMethod.GET)
    public Result findUserById2(@PathVariable("id") Integer id){
        Long userId = id.longValue();
        Optional<User> optional = userService.findUserById2(userId);
        if(optional.isPresent()){
            User user = optional.get();
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","查找用户成功");
            map.put("status",200);
            return Result.success(user,map);
        }else{
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","查找用户为空");
            map.put("status",250);
            return Result.success(null,map);
        }
    }


    /**
     *
     * pageNum  第几页
     * pageSize  每页的数量
     * @return
     */
    @ApiOperation(value = "添加用户")
    @ResponseBody
    @RequestMapping(value = "/admin/addUser",method = RequestMethod.POST)
    public Result addUser(@RequestBody User user){
        User users = userService.addUser(user);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","添加用户成功");
        map.put("status",200);
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setMethod("/admin/addUser");
        sysOperLog.setTitle("添加用户");
        sysOperLog.setJsonResult("添加用户成功");
        sysOperLog.setMethod("POST");
        sysOperLog.setOperIp("127.0.0.1");
        sysOperLog.setOperName("系统日志");
        sysOperLog.setOperTime(new Date());
        sysOperLog.setOperParam("com.vip.dao.admin.domain.User");
        sysOperLog.setOperUrl("/admin/addUser");
        kafkaProducer.fireEvent(sysOperLog);
        return Result.success(user,map);
    }

    @ApiOperation(value = "根据id删除用户")
    @RequestMapping(value = "/admin/deleteUserById/{id}",method = RequestMethod.GET)
    public Result deleteUserById(@PathVariable("id") Integer id) {

        try{

            userService.deleteUserById(id.longValue());
        }catch (Exception e){
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg",e.getMessage());
            map.put("status",500);
            SysOperLog sysOperLog = new SysOperLog();
            sysOperLog.setMethod("/admin/deleteUserById/{id}");
            sysOperLog.setTitle("删除用户");
            sysOperLog.setJsonResult("删除用户失败");
            sysOperLog.setMethod("GET");
            sysOperLog.setOperIp("127.0.0.1");
            sysOperLog.setOperName("系统日志");
            sysOperLog.setOperTime(new Date());
            sysOperLog.setOperParam("com.vip.dao.admin.domain.User.id");
            sysOperLog.setOperUrl("/admin/deleteUserById/{id}");
            kafkaProducer.fireEvent(sysOperLog);
            return Result.success("删除失败",map);
        }
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setMethod("/admin/deleteUserById/{id}");
        sysOperLog.setTitle("删除用户");
        sysOperLog.setJsonResult("删除用户成功");
        sysOperLog.setMethod("GET");
        sysOperLog.setOperIp("127.0.0.1");
        sysOperLog.setOperName("系统日志");
        sysOperLog.setOperTime(new Date());
        sysOperLog.setOperParam("com.vip.dao.admin.domain.User.id");
        sysOperLog.setOperUrl("/admin/deleteUserById/{id}");
        kafkaProducer.fireEvent(sysOperLog);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","删除用户成功");
        map.put("status",200);
        return Result.success(null,map);
    }



    @ApiOperation(value = "为用户分配角色")
    @ResponseBody
    @RequestMapping(value = "/admin/userAddRole",method = RequestMethod.POST)
    public Result roleAdd(Integer uid,String role){
        SysRole sysRole = sysRoleService.findRoleByRole(role);
        User user = userService.findUserById(uid.longValue());
        List<SysRole> roles = user.getRoles();
        roles.add(sysRole);
        user.setRoles(roles);
        User users = userService.addUser(user);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","添加角色成功");
        map.put("status",200);
        /**
         * 日志部分
         */
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setMethod("/admin/userAddRole");
        sysOperLog.setTitle("添加角色");
        sysOperLog.setJsonResult("添加角色成功");
        sysOperLog.setMethod("POST");
        sysOperLog.setOperIp("127.0.0.1");
        sysOperLog.setOperName("系统日志");
        sysOperLog.setOperTime(new Date());
        sysOperLog.setOperParam("com.vip.dao.admin.domain.SysRole");
        sysOperLog.setOperUrl("/admin/userAddRole");
        kafkaProducer.fireEvent(sysOperLog);
        return Result.success(user,map);
    }

    /**
     *
     * pageNum  第几页
     * pageSize  每页的数量
     * @return
     */

    @ApiOperation(value = "查找所有角色")
    @ResponseBody
    @RequestMapping(value = "/admin/findAllRole",method = RequestMethod.GET)
    public Result findAllRole(){
        List<SysRole> sysRoleList = sysRoleService.findAllSysRole();
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","获取角色列表成功");
        map.put("status",200);
        return Result.success(sysRoleList,map);
    }

    /**
     *
     * pageNum  第几页
     * pageSize  每页的数量
     * @return
     */
    @ApiOperation(value = "根据id删除角色")
    @RequestMapping(value = "/admin/deleteRoleById/{id}",method = RequestMethod.GET)
    public Result deleteRoleById(@PathVariable("id") Integer id) {

        try{
            sysRoleService.deleteRoleById(id.longValue());
        }catch (Exception e){
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg",e.getMessage());
            map.put("status",500);
            /**
             * 日志部分
             */
            SysOperLog sysOperLog = new SysOperLog();
            sysOperLog.setMethod("/admin/deleteRoleById/{id}");
            sysOperLog.setTitle("根据id删除角色");
            sysOperLog.setJsonResult("删除失败");
            sysOperLog.setMethod("GET");
            sysOperLog.setOperIp("127.0.0.1");
            sysOperLog.setOperName("系统日志");
            sysOperLog.setOperTime(new Date());
            sysOperLog.setOperParam("com.vip.dao.admin.domain.SysRole.id");
            sysOperLog.setOperUrl("/admin/deleteRoleById/{id}");
            kafkaProducer.fireEvent(sysOperLog);
            return Result.success("删除失败",map);
        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","删除角色成功");
        map.put("status",200);
        /**
         * 日志部分
         */
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setMethod("/admin/deleteRoleById/{id}");
        sysOperLog.setTitle("根据id删除角色");
        sysOperLog.setJsonResult("删除角色成功");
        sysOperLog.setMethod("GET");
        sysOperLog.setOperIp("127.0.0.1");
        sysOperLog.setOperName("系统日志");
        sysOperLog.setOperTime(new Date());
        sysOperLog.setOperParam("com.vip.dao.admin.domain.SysRole.id");
        sysOperLog.setOperUrl("/admin/deleteRoleById/{id}");
        kafkaProducer.fireEvent(sysOperLog);
        return Result.success(null,map);
    }


    /**
     *
     * pageNum  第几页
     * pageSize  每页的数量
     * @return
     */
    @ApiOperation(value = "查找所有权限")
    @RequestMapping(value = "/admin/findAllPermission",method = RequestMethod.POST)
    public Result findAllPermission(@RequestBody QueryInfo queryInfo){
        int pageNum = queryInfo.getPageNum();
        int pageSize = queryInfo.getPageSize();
        Page<SysPermission> page = sysPermissionService.findAllSysPermission(pageNum,pageSize);
        HashMap<String,Object> map = new HashMap<>();
        map.put("msg","获取权限列表成功");
        map.put("status",200);
        return Result.success(page,map);
    }




}
